import axios from 'axios'
const service = axios.create({
    baseURL:'http://localhost:3000',
    timeout:5000,
    headers:{
        'Content-Type':'application/json'
    }
})

//request拦截器
service.interceptors.request.use(
    config =>{
        //在这里设置请求头 请求参数等 return config
        if(localStorage.token){
            config.headers.Authorization = localStorage.token
        }
        return config
    },
    error =>{
        console.log("request拦截器错误",error)
        return Promise.reject(error)
    }
)

//response拦截器
service.interceptors.response.use(
    response=>{
        //在这里处理返回数据
        return response
    },
    error=>{
        const {status} = error.response
       if(status == 401){
        console.log('token失效')
        localStorage.removeItem('token')
       }
        return Promise.reject(error);
    }
)

export default service